<?php
/* Author: Matt Belanger, aka Zurvan on 2+2 Forums
 * Date: January 5, 2010
 * Contributors: 
 */

include('roles.inc');

if (empty($_POST['num_players']) || empty($roles[(int)$_POST['num_players']])) {
  $num_players = 9;
} else {
  $num_players = (int)$_POST['num_players'];
}
if (empty($_POST['iterations'])) {
  $iterations = 1000;
} else if ((int)$_POST['iterations'] <= 10000) {
  $iterations = (int)$_POST['iterations'];
} else {
  $iterations = 10000;
}

if (!in_array($_POST['seer'],array('Yes','No'))) {
  $seer = "No";
} else {
  $seer = $_POST['seer'];
}
$totalWolves = $roles[$num_players]['w'];
$totalVillagers = $roles[$num_players]['v'];
$totalSeers = $roles[$num_players]['s'];

$wolfNKSkill = 0;
$villagerLynchSkill = 0;

$results = array('wolfwins' => 0,
                 'villwins' => 0,
                 'd1wolf' => array('villwin' => 0, 'wolfwin' => 0),
                 'd1vill' => array('villwin' => 0, 'wolfwin' => 0),
                 'd1seer' => array('villwin' => 0, 'wolfwin' => 0),
                 );

$maxDays = ($num_players-1)/2; 
for ($i=1;$i<=$maxDays;$i++) {
  $results["d$i"]["wolflynch"] = 0;
  $results["d$i"]["villlynch"] = 0;
  $results["d$i"]["seerlynch"] = 0;
  $results["endDay$i"]['wolf'] = 0;
  $results["endDay$i"]['vill'] = 0;
}

for ($i=0;$i<$iterations;$i++) {
  $vLive = $totalVillagers;
  $wLive = $totalWolves;
  $sLive = $totalSeers;
  $day = 0;
  $d1 = '';
  while (!isOver($wLive, $vLive, $sLive)) {
    $day++;
    //Calculate the odds a wolf will be lynched.  Using 10,000 cuz PHP only provides integers through rand
    $wolfChance = (($wLive+$villagerLynchSkill)/($wLive+$vLive+$sLive))*10000;
    //Find the odds that a seer is lynched, then add the wolf chance to ease conditional below
    $seerChance = ($sLive/($wLive+$vLive+$sLive))*10000+$wolfChance;
    $lynchNumber = rand(1,10000);

    
    if ($lynchNumber <= $wolfChance) {
      $wLive--;
      $results["d$day"]["wolflynch"]++;
      if ($day == 1) {
        $d1 = "wolf";
      }
      $lynch = "wolf";
    } else if ($lynchNumber <= $seerChance) {
      $sLive--;
      $results["d$day"]["seerlynch"]++;
      if ($day == 1) {
        $d1 = "seer";
      }
      $lynch = "seer";
    } else {
      $vLive--;
      $results["d$day"]["villlynch"]++;
      if ($day == 1) {
        $d1 = "vill";
      }
      $lynch = "vill";
    }
    //print "<p><strong>Day $day Results:</strong><br>Wolf: $wolfChance Seer: $seerChance Lynch Number: $lynchNumber Lynch: $lynch NK: $nk</p>";
    //print "<p><strong>Game Status:</strong><br>Wolves: $wLive  Villager: $vLive  Seers: $sLive</p><br>";

    //If the gam is over, record the stats
    if (isOver($wLive, $vLive, $sLive)) {
      if ($wLive > 0) {
        $results['wolfwins']++;
        $results["d1$d1"]['wolfwin']++;
        $results["endDay$day"]['wolf']++;
      } else {
        $results['villwins']++;
        $results["d1$d1"]['villwin']++;
        $results["endDay$day"]['vill']++;
      }
    } else {
      //Find the night kill
      $seerNKChance = (($sLive+$wolfNKSkill)/($vLive+$sLive))*10000;
      $nkNumber = rand(1,10000);
      if ($nkNumber <= $seerNKChance) {
        $sLive--;
        $nk = "Seer";
      } else {
        $vLive--;
        $nk = "Villager";
      }
    }
  }
}
?>
<html>
<head>
  <title>Lynchinator - Results</title>
  <link href="lynchinate.css" rel="stylesheet" />
</head>
<body>
<fieldset>
  <legend>Parameters</legend>
  <label>Total Players:</label> <?= $num_players; ?><br>
  <label>Villagers: </label> <?= $totalVillagers; ?><br>
  <label>Seers: </label> <?= $totalSeers; ?><br>
  <label>Wolves: </label> <?= $totalWolves; ?><br>
  <label>Iterations: </label> <?= $iterations; ?>
</fieldset>
<fieldset>
  <legend>Results</legend>
  <table>
    <tr><th class="data_header">&nbsp;</th><th>Total Games</th><th>Village Wins</th><th>Villa Win %</th><th>Wolf Wins</th><th>Wolf Win %</th></tr>
    <tr>
      <td class="data_header">Total:</td>
      <td><?= $iterations; ?></td>
      <td><?= $results['villwins']; ?></td>
      <td><?= ($results['villwins']/$iterations)*100; ?>%</td>
      <td><?= $results['wolfwins']; ?></td>
      <td><?= ($results['wolfwins']/$iterations)*100; ?>%</td>
    </tr>
    <tr>
      <td class="data_header">D1 Villager Lynches:</td>
      <td><?php $secTotal = $results['d1vill']['villwin']+$results['d1vill']['wolfwin']; print $secTotal;?></td>
      <td><?= $results['d1vill']['villwin']; ?></td>
      <td><?= number_format(($results['d1vill']['villwin']/$secTotal)*100,2); ?>%</td>
      <td><?= $results['d1vill']['wolfwin']; ?></td>
      <td><?= number_format(($results['d1vill']['wolfwin']/$secTotal)*100,2); ?>%</td>
    </tr>
    <tr>
      <td class="data_header">D1 Seer Lynches:</td>
      <td><?php $secTotal = $results['d1seer']['villwin']+$results['d1seer']['wolfwin']; print $secTotal;?></td>
      <td><?= $results['d1seer']['villwin']; ?></td>
      <td><?= number_format(($results['d1seer']['villwin']/$secTotal)*100,2); ?>%</td>
      <td><?= $results['d1seer']['wolfwin']; ?></td>
      <td><?= number_format(($results['d1seer']['wolfwin']/$secTotal)*100,2); ?>%</td>
    </tr>
    <tr>
      <td class="data_header">D1 Wolf Lynches:</td>
      <td><?php $secTotal = $results['d1wolf']['villwin']+$results['d1wolf']['wolfwin']; print $secTotal;?></td>
      <td><?= $results['d1wolf']['villwin']; ?></td>
      <td><?= number_format(($results['d1wolf']['villwin']/$secTotal)*100,2); ?>%</td>
      <td><?= $results['d1wolf']['wolfwin']; ?></td>
      <td><?= number_format(($results['d1wolf']['wolfwin']/$secTotal)*100,2); ?>%</td>
    </tr>
  </table>
 </fieldset>
<fieldset>
  <legend>Ending by Game Day</legend>
  <table>
    <tr><th>Game Day</th><th>Total Games</th><th>Wolf Wins today</th><th>Village Wins Today</th></tr>
    <?php
      $secTotal = $iterations;
      for ($i=1;$i<=$maxDays;$i++) {
    ?>
    <tr>
      <td><?= $i; ?></td>
      <td><?= $secTotal; ?></td>
      <td><?php print $results["endDay$i"]['wolf']; $secTotal -= $results["endDay$i"]['wolf']; ?></td>
      <td><?php print $results["endDay$i"]['vill']; $secTotal -= $results["endDay$i"]['vill']; ?></td>
    </tr>
    <?php } ?>
  </table>
</body>
</html>